package org.sbbs.app.license.webapp.action;

import org.sbbs.app.license.model.ProductInfo;
import org.sbbs.app.license.model.VenderInfo;
import org.sbbs.app.license.service.ProductInfoManager;
import org.sbbs.app.license.service.VenderInfoManager;
import org.sbbs.base.webapp.action.BaseMaintainAction;

public class ProductInfoMaintainAction
    extends BaseMaintainAction<ProductInfo, String> {
    /**
     * 
     */
    private static final long serialVersionUID = 4898653670229709972L;

    private ProductInfo model;

    private ProductInfoManager productInfoManager;

    private VenderInfoManager venderInfoManager;

    public String edit() {
        this.model = this.productInfoManager.find( this.getId() );
        this.setEditTypeEdit();
        return SUCCESS;
    }

    public String add() {
        this.model = new ProductInfo();
        VenderInfo vender = this.venderInfoManager.findAll().get( 0 );
        this.model.setVenderInfo( vender );
        this.setEditTypeAdd();
        return SUCCESS;
    }

    public String save() {
        try {
            // HttpServletRequest req = this.getRequest();
            // boolean isNew = ( this.getEditType() == 1 );
            this.productInfoManager.save( this.getModel() );
            this.setAjaxMessage( getText( ( isNew() ) ? "productInfo.added" : "productInfo.updated", "no msg key found,save successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.saved", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }
        return SUCCESS;
    }

    public String delete() {
        try {
            String[] sIds = this.getIds().split( "," );
            /*
             * Long[] lIds = new Long[sIds.length]; for ( int i = 0; i < sIds.length; i++ ) { lIds[i] = Long.parseLong(
             * sIds[i] ); }
             */

            this.productInfoManager.removeByIds( sIds );

            this.setAjaxMessage( getText( "productInfo.deleted", "no msg key found,delete successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.deleted", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }

        return SUCCESS;
    }

    public String disable() {
        try {
            String[] sIds = this.getIds().split( "," );
            this.productInfoManager.disableProductInfo( sIds );
            this.setAjaxMessage( getText( "productInfo.successDisabled", "no msg key found,disabled successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.common", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }

        return SUCCESS;
    }

    public String enable() {
        try {
            String[] sIds = this.getIds().split( "," );
            this.productInfoManager.enableProductInfo(  sIds );
            this.setAjaxMessage( getText( "productInfo.successEnabled", "no msg key found,enabled successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.common", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }

        return SUCCESS;
    }

    public ProductInfo getModel() {
        return model;
    }

    public void setModel( ProductInfo model ) {
        this.model = model;
    }

    public ProductInfoManager getProductInfoManager() {
        return productInfoManager;
    }

    public void setProductInfoManager( ProductInfoManager productInfoManager ) {
        this.productInfoManager = productInfoManager;
    }

    public VenderInfoManager getVenderInfoManager() {
        return venderInfoManager;
    }

    public void setVenderInfoManager( VenderInfoManager venderInfoManager ) {
        this.venderInfoManager = venderInfoManager;
    }

}
